package com.jinlejia.bbs.logic;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.http.entity.InviteUserEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * @author 王进 on 2019/3/28.
 */
//邀请回答
public class InvitedUserListLogic {
    private Context context;
    private LogicCallBack callBack;
    private ArrayList<BBSUserBean> dataList;
    public InvitedUserListLogic(Context context, ArrayList<BBSUserBean> dataList,LogicCallBack callBack){
        this.context = context;
        this.callBack = callBack;
        this.dataList = dataList;
    }


    private int pageIndex;
    public void refresh(String tagName){
        pageIndex = 1;
        loadData(tagName,pageIndex);
    }

    public void load(String tagName){
        pageIndex = pageIndex+1;
        loadData(tagName,pageIndex);
    }

    private void loadData(String tagName,final int pageIndex){
        JSONObject json = new JSONObject();
        json.put("tagName",tagName);
        json.put("pageIndex",pageIndex);
        json.put("pageSize",20);
        httpCallForm(BBSConfig.listUserByTagName,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        TypeReference<PageEntity<InviteUserEntity>> reference = new TypeReference<PageEntity<InviteUserEntity>>() {};
                        PageEntity<InviteUserEntity> response = parse.parsePageEntity(reference);
                        List<InviteUserEntity> data = response.getData();
                        ArrayList<BBSUserBean> list = new ArrayList<>();
                        for (InviteUserEntity entity:data) {
                            BBSUserBean bean = new BBSUserBean();
                            bean.setUserPin(entity.getPin());
                            bean.setUserName(entity.getUserName());
                            bean.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                            bean.setUserDuty(entity.getStoreName());
                            bean.setUserAddress(entity.getStoreName());

                            bean.setUserLevel(entity.getUserLevel());
                            bean.setAttitudesCount(entity.getSupportCount());

                            //标签
                            ArrayList<TagBean> tags = new ArrayList<>();
                            String entityTag = entity.getTagName();
                            if (!TextUtils.isEmpty(entityTag)){
                                String[] split = entityTag.split(",");
                                for (String tag:split) {
                                    TagBean tagBean = new TagBean();
                                    tagBean.setValueName(tag);
                                    tagBean.setValue(tag);
                                    tags.add(tagBean);
                                }
                            }
                            bean.setTags(tags);
                            list.add(bean);
                        }



                        if (pageIndex==1){
                            dataList.clear();
                        }
                        dataList.addAll(list);
                        callBack.onResult(response.getNextPageAvailable());
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }
}
